package com.esri.core.geometry;

import com.esri.core.geometry.Geometry;

/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes.dex */
public class QuadTree {
    Envelope2D a;
    QuadTreeIterator b;
    SimpleQuadTreeIterator c;
    AttributeStreamOfInt32 d;
    AttributeStreamOfDbl e;
    AttributeStreamOfInt32 f;

    QuadTree() {
    }

    /* loaded from: classes.dex */
    static class QuadTreeIterator {
        boolean a;
        Point2D b;
        Point2D c;
        Envelope2D d;
        Envelope2D e;
        int h;
        QuadTree j;
        boolean m;
        AttributeStreamOfInt32 n;
        AttributeStreamOfInt32 o;
        int f = -2;
        int g = -2;
        AttributeStreamOfInt32 k = new AttributeStreamOfInt32(0);
        AttributeStreamOfInt8 l = new AttributeStreamOfInt8(0);
        byte i = 0;

        QuadTreeIterator(boolean z, QuadTree quadTree, Segment segment, int i) {
            this.j = quadTree;
            this.h = i;
            this.m = z;
            boolean z2 = segment.getType() == Geometry.Type.LINE;
            this.a = z2;
            if (z2) {
                this.b = segment.a();
                this.c = segment.j();
            } else {
                Envelope2D envelope2D = new Envelope2D();
                this.d = envelope2D;
                segment.queryEnvelope2D(envelope2D);
            }
            if (this.m || quadTree.d.get(6) >= this.h) {
                if (this.m) {
                    AttributeStreamOfInt32 attributeStreamOfInt32 = new AttributeStreamOfInt32(0);
                    this.o = attributeStreamOfInt32;
                    attributeStreamOfInt32.resize(this.j.f.size() / 3, -1.0d);
                }
                Envelope2D envelope2D2 = new Envelope2D();
                this.e = envelope2D2;
                envelope2D2.setCoords(this.j.a);
                this.k.add(0);
                this.l.add((byte) 0);
            }
        }

        void a(Segment segment, int i) {
            this.f = -2;
            this.g = -2;
            this.h = i;
            this.k.resize(0);
            this.l.resize(0);
            this.i = (byte) 0;
            boolean z = segment.getType() == Geometry.Type.LINE;
            this.a = z;
            if (z) {
                this.b = segment.a();
                this.c = segment.j();
            } else {
                segment.queryEnvelope2D(this.d);
            }
            if (this.m || this.j.d.get(6) >= this.h) {
                this.e.setCoords(this.j.a);
                this.k.add(0);
                this.l.add((byte) 0);
            }
        }

        static void a(byte b, byte b2, Envelope2D envelope2D) {
            double width = envelope2D.getWidth() * 0.6666666666666666d;
            double height = envelope2D.getHeight() * 0.6666666666666666d;
            if (b2 == 1) {
                envelope2D.xmax += width;
                envelope2D.xmin += width;
            } else if (b2 == 2) {
                if (b != 0) {
                    if (b != 1) {
                        return;
                    }
                    envelope2D.ymax += height;
                    envelope2D.ymin += height;
                    return;
                }
                envelope2D.ymax += height;
                envelope2D.ymin += height;
                envelope2D.xmax += width;
                envelope2D.xmin += width;
            } else if (b2 != 3) {
            } else {
                if (b == 0) {
                    envelope2D.ymax += height;
                    envelope2D.ymin += height;
                } else if (b != 1) {
                    if (b != 2) {
                        return;
                    }
                    envelope2D.xmax -= width;
                    envelope2D.xmin -= width;
                } else {
                    envelope2D.ymax += height;
                    envelope2D.ymin += height;
                    envelope2D.xmax -= width;
                    envelope2D.xmin -= width;
                }
            }
        }

        static void a(byte b, Envelope2D envelope2D) {
            double width = envelope2D.getWidth();
            double height = envelope2D.getHeight();
            Point2D center = envelope2D.getCenter();
            if (b == 0) {
                envelope2D.ymax = center.y + (height / 3.0d);
                envelope2D.ymin = center.y - height;
                envelope2D.xmax = center.x + (width / 3.0d);
                envelope2D.xmin = center.x - width;
            } else if (b == 1) {
                envelope2D.ymax = center.y + (height / 3.0d);
                envelope2D.ymin = center.y - height;
                envelope2D.xmax = center.x + width;
                envelope2D.xmin = center.x - (width / 3.0d);
            } else if (b == 2) {
                envelope2D.ymax = center.y + height;
                envelope2D.ymin = center.y - (height / 3.0d);
                envelope2D.xmax = center.x + width;
                envelope2D.xmin = center.x - (width / 3.0d);
            } else if (b != 3) {
            } else {
                envelope2D.ymax = center.y + height;
                envelope2D.ymin = center.y - (height / 3.0d);
                envelope2D.xmax = center.x + (width / 3.0d);
                envelope2D.xmin = center.x - width;
            }
        }

        /* JADX WARN: Removed duplicated region for block: B:103:0x0331  */
        /* JADX WARN: Removed duplicated region for block: B:117:0x03a5  */
        /* JADX WARN: Removed duplicated region for block: B:122:0x03b8  */
        /* JADX WARN: Removed duplicated region for block: B:147:0x03b6 A[SYNTHETIC] */
        /* JADX WARN: Removed duplicated region for block: B:29:0x00f3  */
        /* JADX WARN: Removed duplicated region for block: B:32:0x00fc  */
        /* JADX WARN: Removed duplicated region for block: B:61:0x0219  */
        /* JADX WARN: Removed duplicated region for block: B:77:0x0296  */
        /* JADX WARN: Removed duplicated region for block: B:81:0x02a2  */
        /* JADX WARN: Removed duplicated region for block: B:98:0x0324  */
        /*
            Code decompiled incorrectly, please refer to instructions dump.
            To view partially-correct code enable 'Show inconsistent code' option in preferences
        */
        int a() {
            /*
                Method dump skipped, instructions count: 1181
                To view this dump change 'Code comments level' option to 'DEBUG'
            */
            throw new UnsupportedOperationException("Method not decompiled: com.esri.core.geometry.QuadTree.QuadTreeIterator.a():int");
        }

        void b() {
            if (!this.m || this.f == -1) {
                throw new GeometryException("invalid call");
            }
            int i = this.j.d.get(this.k.getLast() + 4);
            int i2 = this.j.d.get(this.k.getLast() + 5);
            int i3 = this.j.f.get(this.f + 1);
            int i4 = this.j.f.get(this.f + 2);
            int i5 = this.f;
            if (i == i5) {
                if (i4 != -1) {
                    this.j.f.set(i4 + 1, -1);
                }
                this.j.d.set(this.k.getLast() + 4, i4);
                if (i == i2) {
                    this.j.d.set(this.k.getLast() + 5, -1);
                }
            } else if (i2 == i5) {
                if (i3 != -1) {
                    this.j.f.set(i3 + 2, -1);
                }
                this.j.d.set(this.k.getLast() + 5, i3);
            } else {
                if (i4 != -1) {
                    this.j.f.set(i4 + 1, i3);
                }
                this.j.f.set(i3 + 2, i4);
            }
            this.g = i4;
            this.j.f.set(this.f + 1, -1);
            this.j.f.set(this.f + 2, -1);
            if (this.n == null) {
                this.n = new AttributeStreamOfInt32(0);
            }
            this.n.add(this.f);
            this.o.set(this.f / 3, -1);
        }

        void a(int i, Envelope2D envelope2D, int i2) {
            if (!this.e.contains(envelope2D)) {
                throw new GeometryException("invalid call");
            }
            AttributeStreamOfInt32 attributeStreamOfInt32 = this.n;
            if (attributeStreamOfInt32 != null && attributeStreamOfInt32.size() > 0) {
                int last = this.n.getLast();
                this.n.removeLast();
                this.j.a(i, envelope2D, this.i, this.e, this.k.getLast(), last);
                this.o.set(last / 3, i2);
                return;
            }
            this.j.a(i, envelope2D, this.i, this.e, this.k.getLast(), -1);
            this.o.add(i2);
        }
    }

    /* loaded from: classes.dex */
    static class SimpleQuadTreeIterator {
        Envelope2D a;
        QuadTree e;
        int b = -2;
        int c = -2;
        AttributeStreamOfInt32 f = new AttributeStreamOfInt32(0);
        AttributeStreamOfInt8 g = new AttributeStreamOfInt8(0);
        byte d = 0;

        SimpleQuadTreeIterator(QuadTree quadTree) {
            this.e = quadTree;
            Envelope2D envelope2D = new Envelope2D();
            this.a = envelope2D;
            envelope2D.setCoords(this.e.a);
            this.f.add(0);
            this.g.add((byte) 0);
        }

        void a() {
            this.b = -2;
            this.c = -2;
            this.f.resize(0);
            this.g.resize(0);
            this.d = (byte) 0;
            this.a.setCoords(this.e.a);
            this.f.add(0);
            this.g.add((byte) 0);
        }

        int b() {
            int i;
            int i2;
            int i3;
            int i4;
            Envelope2D envelope2D;
            Envelope2D envelope2D2;
            Envelope2D envelope2D3;
            Envelope2D envelope2D4;
            double d;
            int i5;
            Envelope2D envelope2D5;
            int i6;
            double d2;
            Envelope2D envelope2D6;
            int i7;
            int i8;
            Envelope2D envelope2D7;
            int i9;
            Envelope2D envelope2D8;
            Envelope2D envelope2D9;
            int i10;
            Envelope2D envelope2D10;
            Envelope2D envelope2D11;
            Envelope2D envelope2D12;
            Envelope2D envelope2D13;
            int i11 = -1;
            if (this.f.size() == 0) {
                return -1;
            }
            int i12 = this.c;
            byte b = 4;
            if (i12 == -2) {
                if (this.b == -2) {
                    this.b = this.e.d.get(4);
                } else {
                    this.b = this.e.f.get(this.b + 2);
                }
            } else {
                this.b = i12;
                this.c = -2;
            }
            Point2D point2D = new Point2D();
            Envelope2D envelope2D14 = new Envelope2D();
            Envelope2D envelope2D15 = new Envelope2D();
            Envelope2D envelope2D16 = new Envelope2D();
            Envelope2D envelope2D17 = new Envelope2D();
            while (this.b == i11) {
                int last = this.f.getLast();
                int i13 = this.e.d.get(last);
                int i14 = this.e.d.get(last + 1);
                int i15 = this.e.d.get(last + 2);
                int i16 = this.e.d.get(last + 3);
                this.a.queryCenter(point2D);
                Envelope2D envelope2D18 = envelope2D14;
                double width = this.a.getWidth() / 8.0d;
                double height = this.a.getHeight() / 8.0d;
                this.g.add(b);
                if (i13 != -1) {
                    envelope2D3 = envelope2D15;
                    i = i16;
                    i2 = i15;
                    i3 = i13;
                    i4 = i14;
                    envelope2D = envelope2D16;
                    envelope2D2 = envelope2D17;
                    envelope2D18.setCoords(point2D.x, point2D.y, this.a.xmax, this.a.ymax);
                    envelope2D4 = envelope2D18;
                    d = width;
                    envelope2D4.inflate(d, height);
                    this.f.add(i3);
                    this.g.add((byte) 0);
                } else {
                    i = i16;
                    i2 = i15;
                    i3 = i13;
                    i4 = i14;
                    envelope2D = envelope2D16;
                    envelope2D2 = envelope2D17;
                    envelope2D3 = envelope2D15;
                    envelope2D4 = envelope2D18;
                    d = width;
                }
                int i17 = -1;
                if (i4 != -1) {
                    double d3 = d;
                    envelope2D5 = envelope2D4;
                    i5 = i3;
                    d2 = d3;
                    envelope2D3.setCoords(this.a.xmin, point2D.y, point2D.x, this.a.ymax);
                    envelope2D6 = envelope2D3;
                    envelope2D6.inflate(d2, height);
                    i6 = i4;
                    this.f.add(i6);
                    this.g.add((byte) 1);
                    i7 = i2;
                    i17 = -1;
                } else {
                    i5 = i3;
                    envelope2D5 = envelope2D4;
                    i6 = i4;
                    d2 = d;
                    envelope2D6 = envelope2D3;
                    i7 = i2;
                }
                if (i7 != i17) {
                    envelope2D8 = envelope2D;
                    i8 = i7;
                    i9 = i6;
                    envelope2D7 = envelope2D6;
                    envelope2D8.setCoords(this.a.xmin, this.a.ymin, point2D.x, point2D.y);
                    envelope2D8.inflate(d2, height);
                    this.f.add(i8);
                    this.g.add((byte) 2);
                } else {
                    i8 = i7;
                    envelope2D7 = envelope2D6;
                    i9 = i6;
                    envelope2D8 = envelope2D;
                }
                int i18 = i;
                if (i18 != -1) {
                    envelope2D9 = envelope2D8;
                    envelope2D2.setCoords(point2D.x, this.a.ymin, this.a.xmax, point2D.y);
                    envelope2D10 = envelope2D2;
                    envelope2D10.inflate(d2, height);
                    i10 = i18;
                    this.f.add(i10);
                    this.g.add((byte) 3);
                } else {
                    envelope2D9 = envelope2D8;
                    i10 = i18;
                    envelope2D10 = envelope2D2;
                }
                int last2 = this.f.getLast();
                if (last2 == i5) {
                    this.d = (byte) (this.d + 1);
                    envelope2D11 = envelope2D5;
                    this.a.setCoords(envelope2D11);
                    envelope2D13 = envelope2D9;
                    envelope2D12 = envelope2D7;
                } else {
                    envelope2D11 = envelope2D5;
                    if (last2 == i9) {
                        this.d = (byte) (this.d + 1);
                        envelope2D12 = envelope2D7;
                        this.a.setCoords(envelope2D12);
                        envelope2D13 = envelope2D9;
                    } else {
                        envelope2D12 = envelope2D7;
                        if (last2 == i8) {
                            this.d = (byte) (this.d + 1);
                            envelope2D13 = envelope2D9;
                            this.a.setCoords(envelope2D13);
                        } else {
                            envelope2D13 = envelope2D9;
                            if (last2 == i10) {
                                this.d = (byte) (this.d + 1);
                                this.a.setCoords(envelope2D10);
                            } else {
                                this.f.removeLast();
                                if (this.f.size() == 0) {
                                    return -1;
                                }
                                this.g.removeLast();
                                byte last3 = this.g.getLast();
                                this.g.removeLast();
                                byte last4 = this.g.getLast();
                                while (last4 == 4) {
                                    this.f.removeLast();
                                    if (this.f.size() == 0) {
                                        return -1;
                                    }
                                    this.d = (byte) (this.d - 1);
                                    QuadTreeIterator.a(last3, this.a);
                                    this.g.removeLast();
                                    last3 = this.g.getLast();
                                    this.g.removeLast();
                                    last4 = this.g.getLast();
                                }
                                int i19 = 2;
                                while (true) {
                                    AttributeStreamOfInt8 attributeStreamOfInt8 = this.g;
                                    if (attributeStreamOfInt8.a(attributeStreamOfInt8.size() - i19) == 4) {
                                        break;
                                    }
                                    i19++;
                                }
                                this.e.d.set(this.f.get(this.f.size() - i19) + last3, -1);
                                QuadTreeIterator.a(last4, last3, this.a);
                                this.b = this.e.d.get(this.f.getLast() + 4);
                                envelope2D14 = envelope2D11;
                                envelope2D17 = envelope2D10;
                                envelope2D15 = envelope2D12;
                                envelope2D16 = envelope2D13;
                                i11 = -1;
                                b = 4;
                            }
                        }
                    }
                }
                this.b = this.e.d.get(this.f.getLast() + 4);
                envelope2D14 = envelope2D11;
                envelope2D17 = envelope2D10;
                envelope2D15 = envelope2D12;
                envelope2D16 = envelope2D13;
                i11 = -1;
                b = 4;
            }
            return this.e.f.get(this.b);
        }

        void c() {
            if (this.b == -1) {
                throw new GeometryException("invalid call");
            }
            int i = this.e.d.get(this.f.getLast() + 4);
            int i2 = this.e.d.get(this.f.getLast() + 5);
            int i3 = this.e.f.get(this.b + 1);
            int i4 = this.e.f.get(this.b + 2);
            int i5 = this.b;
            if (i == i5) {
                if (i4 != -1) {
                    this.e.f.set(i4 + 1, -1);
                }
                this.e.d.set(this.f.getLast() + 4, i4);
                if (i == i2) {
                    this.e.d.set(this.f.getLast() + 5, -1);
                }
            } else if (i2 == i5) {
                if (i3 != -1) {
                    this.e.f.set(i3 + 2, -1);
                }
                this.e.d.set(this.f.getLast() + 5, i3);
            } else {
                if (i4 != -1) {
                    this.e.f.set(i4 + 1, i3);
                }
                this.e.f.set(i3 + 2, i4);
            }
            this.c = i4;
            this.e.f.set(this.b + 1, -1);
            this.e.f.set(this.b + 2, -1);
            if (this.e.b.n == null) {
                this.e.b.n = new AttributeStreamOfInt32(0);
            }
            this.e.b.n.add(this.b);
            this.e.b.o.set(this.b / 3, -1);
        }
    }

    QuadTreeIterator a(boolean z, Segment segment, int i) {
        QuadTreeIterator quadTreeIterator = this.b;
        if (quadTreeIterator == null) {
            this.b = new QuadTreeIterator(z, this, segment, i);
        } else {
            quadTreeIterator.a(segment, i);
        }
        return this.b;
    }

    SimpleQuadTreeIterator a() {
        SimpleQuadTreeIterator simpleQuadTreeIterator = this.c;
        if (simpleQuadTreeIterator == null) {
            this.c = new SimpleQuadTreeIterator(this);
        } else {
            simpleQuadTreeIterator.a();
        }
        return this.c;
    }

    private void a(int i, int i2) {
        AttributeStreamOfInt32 attributeStreamOfInt32 = this.d;
        int i3 = i + 6;
        attributeStreamOfInt32.set(i3, Math.max(i2, attributeStreamOfInt32.get(i3)));
    }

    public void ResetQuadTreeIterator(Segment segment, int i) {
        this.b.a(segment, i);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* JADX WARN: Code restructure failed: missing block: B:50:0x01ae, code lost:
        r7 = -1;
        r13 = -1.0d;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    public void a(int r28, com.esri.core.geometry.Envelope2D r29, byte r30, com.esri.core.geometry.Envelope2D r31, int r32, int r33) {
        /*
            Method dump skipped, instructions count: 557
            To view this dump change 'Code comments level' option to 'DEBUG'
        */
        throw new UnsupportedOperationException("Method not decompiled: com.esri.core.geometry.QuadTree.a(int, com.esri.core.geometry.Envelope2D, byte, com.esri.core.geometry.Envelope2D, int, int):void");
    }
}
